from time import sleep
from selenium import webdriver

driver = webdriver.Chrome()

print("未访问网站时的cookie信息: %s" % driver.get_cookies())
sleep(1)
driver.get("https://home.51cto.com/index/?from_service=www&reback=https://www.51cto.com/")
print("访问51cto后的cookie信息: %s" % driver.get_cookies())
driver.maximize_window()
sleep(1)
driver.find_element_by_class_name("password_corner").click()
driver.find_element_by_xpath("//*[@id='login-base']/div[2]/span[2]").click()
driver.find_element_by_name("LoginForm[username]").send_keys("18718658584")
driver.find_element_by_id("loginform-password").send_keys("w0120221111")
driver.find_element_by_class_name("loginbtn_P").click()
CtoCookies1 = driver.get_cookies()
print("登录51cto后的cookie信息: %s" % CtoCookies1)
print("获取特定cookie：'Hm_lvt_844390da7774b6a92b34d40f8e16f5ac': %s" % driver.get_cookie(name='Hm_lvt_844390da7774b6a92b34d40f8e16f5ac'))
# 添加cookie
driver.add_cookie({'domain': 'home.51cto.com', 'name': 'wyh', 'value': '123456'})
CtoCookies2 = driver.get_cookies()
print("——"*100 + "\n")
for cookie in CtoCookies2:
    print("%s/%s/%s" % (cookie['name'], cookie['domain'], cookie['value']))

# 删除cookie
driver.delete_cookie({'domain': 'home.51cto.com'})
CtoCookies3 = driver.get_cookies()
print("——"*100 + "\n")
for cookie in CtoCookies3:
    print("%s/%s/%s" % (cookie['name'], cookie['domain'], cookie['value']))
print("——"*100 + "\n")
# 删除所有cookie
DeletedCookie = driver.delete_all_cookies()
print("删除所有cookie信息: %s" % DeletedCookie)
sleep(1)
driver.quit()
